﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace homework10
{
public  class Program
    {
        static void Main(string[] args)
        {
            OrderService orderService = new OrderService();
            //创建第一个订单
            Order order1 = new Order
            {
                OrderId = 1,
                Customer = "Customer1",
                Details = new List<OrderDetails>
              { new OrderDetails
              { ProductName = "Product1",
                  Quantity = 2,
                  UnitPrice = 10
              }
              }
            };
            //将第一个订单加入队列
            orderService.AddOrder(order1);

            //相同订单不能重复添加测试：
            //orderService.AddOrder(order1);

            //创建第二个订单
            Order order2 = new Order
            {
                OrderId = 2,
                Customer = "Customer2",
                Details = new List<OrderDetails>
              { new OrderDetails
              { ProductName = "Product2",
                  Quantity = 3,
                  UnitPrice = 20
              }
              }
            };
            //将第二个订单加入队列
            orderService.AddOrder(order2);

            //创建第三个订单
            Order order3 = new Order
            {
                OrderId = 3,
                Customer = "Customer3",
                Details = new List<OrderDetails>
              { new OrderDetails
              { ProductName = "Product3",
                  Quantity = 4,
                  UnitPrice = 5
              }
              }
            };
            //将第三个订单加入队列
            orderService.AddOrder(order3);

            //创建第四个订单
            Order order4 = new Order
            {
                OrderId = 4,
                Customer = "Customer1",
                Details = new List<OrderDetails>
              { new OrderDetails
              { ProductName = "Product4",
                  Quantity = 2,
                  UnitPrice = 5
              }
              }
            };
            //将第四个订单加入队列
            orderService.AddOrder(order4);
            //将所有的订单序列化为xml文件
            orderService.Export("ORDERS.xml");
            //清空订单队列
            orderService = new OrderService();
            //将订单从xml文件中载入
            orderService.Import("ORDERS.xml");
            //查看订单信息
            Console.WriteLine("导出订单信息如下：");
            //订单输出顺序按照订单号ORDERID排序
            foreach(Order order in orderService.QueryOrders(o=>true) )
            {
                Console.WriteLine(order.ToString());
                foreach(OrderDetails detail in order.Details)
                {
                    Console.WriteLine(detail.ToString());
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
       public  class OrderDetails
        {
            public string ProductName { get; set; }//订单产品名称
            public int Quantity { get; set; }//订单数量
            public double UnitPrice { get; set; }//订单单价
            public override bool Equals(object obj)//重写equals方法
            {
                if (!(obj is OrderDetails)) return false;
                OrderDetails other = (OrderDetails)obj;
                return this.ProductName == other.ProductName;
            }
            public override int GetHashCode()//重写gethashcode函数
            {
                return ProductName.GetHashCode();
            }
            public override string ToString()//重写tostring方法改为显示订单详细信息
            {
                return $"Product: {ProductName}, Quantity: {Quantity}, Unit Price: {UnitPrice}";
            }
        }
       public  class Order
        {
            public int OrderId { get; set; }//订单号
            public string Customer { get; set; }//订单客户
            public List<OrderDetails> Details { get; set; }
            public double TotalAmount => Details.Sum(detail => detail.Quantity * detail.UnitPrice);//计算订单的总金额
            public override bool Equals(object obj)
            {
                if (!(obj is Order)) return false;
                Order other = (Order)obj;
                return this.OrderId == other.OrderId;
            }
            public override int GetHashCode()
            {
                return OrderId.GetHashCode();
            }
            public override string ToString()
            {
                return $"Order  ID:{OrderId},Customer:{Customer},Total Amount:{TotalAmount}";
            }
        }
       public class OrderService
        {
            public List<Order> orders = new List<Order>();
            public void AddOrder(Order order)//像list添加订单
            {
                if (orders.Contains(order))
                    throw new InvalidCastException("Order already exist");
                orders.Add(order);
            }
            public void RemoveOrder(int orderId)//根据订单号从list删除订单
            {
                Order order = orders.FirstOrDefault(o => o.OrderId == orderId);
                if (order == null)
                    throw new KeyNotFoundException("order not found");
                orders.Remove(order);
            }
            public List<Order> QueryOrders(Func<Order, bool> predicate)//根据具体查询条件查询订单
            {
                return orders
                       .Where(predicate)
                       .OrderBy(order => order.OrderId)
                       .ToList();
            }
            public void SortOrders(Func<Order, object> KeySelector)//根据主要排序条件对订单进行排序
            {
                orders = orders
                       .OrderBy(KeySelector)
                       .ToList();
            }
            public void Export(string filepath)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
                using (TextWriter writer= new StreamWriter(filepath))
                {
                    serializer.Serialize(writer, orders);
                }
                Console.WriteLine("已导入至xml文件中");
            }
            public void Import(string filepath)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
                using (FileStream file=new FileStream(filepath,FileMode.Open))
                {
                    orders = (List<Order>)serializer.Deserialize(file);
                }
                Console.WriteLine("已从xml文件中导出订单");
            }
        }
    }
}
